package com.dropdb.demo.controller.admin;

import com.dropdb.demo.bean.Ad;
import com.dropdb.demo.bean.Topic;
import com.dropdb.demo.bean.vo.BaseData;
import com.dropdb.demo.bean.vo.BaseRespVO;
import com.dropdb.demo.service.PromotionService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author qugr
 * @create 2020-06-26 16:53
 */
@RestController
@RequestMapping("admin/topic")
public class AdminPromotionTopicController {

    @Autowired
    PromotionService promotionService;

    @RequestMapping("list")
    @RequiresPermissions(value = {"*", "admin:topic:list"}, logical = Logical.OR)
    public BaseRespVO queryTopics(Integer page, Integer limit, String title, String subtitle, String sort, String order){
        BaseData baseData = promotionService.queryTopics(page, limit, title, subtitle, sort, order);
        return BaseRespVO.ok(baseData);
    }

    @RequestMapping("create")
    @RequiresPermissions(value = {"*", "admin:topic:create"}, logical = Logical.OR)
    public BaseRespVO createTopic(@RequestBody Topic topic){
        int insert = promotionService.insertTopic(topic);
        if (insert != 0) {
            Topic topicVO = promotionService.queryTopic(topic.getId());
            return BaseRespVO.ok(topicVO);
        }
        return BaseRespVO.error(500,"添加失败");
    }

    @RequestMapping("delete")
    @RequiresPermissions(value = {"*", "admin:topic:delete"}, logical = Logical.OR)
    public BaseRespVO deleteTopic(@RequestBody Topic topic){
        int delete = promotionService.deleteTopic(topic.getId());
        if (delete != 1) {
            return BaseRespVO.error(500,"删除失败");
        }
        return BaseRespVO.ok();
    }

    @RequestMapping("update")
    @RequiresPermissions(value = {"*", "admin:topic:update"}, logical = Logical.OR)
    public BaseRespVO updateTopic(@RequestBody Topic topic){
        int update = promotionService.updateTopic(topic);
        if (update != 1) {
            return BaseRespVO.error(500,"更新失败");
        }
        Topic queryTopic = promotionService.queryTopic(topic.getId());
        return BaseRespVO.ok(queryTopic);
    }

}
